package com.vhall.component.service.rbac.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.rbac.RbacUserGroupMapper;
import com.vhall.component.entity.rbac.UserGroupEntity;
import com.vhall.component.entity.rbac.vo.GroupAddUserReqVO;
import com.vhall.component.entity.rbac.vo.GroupDeleteUserReqVO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.rbac.RbacUserGroupService;
import com.vhall.component.service.rbac.constant.AccessConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author dalong
 * @date 2021-05-10 18:15
 */
@Service
@Slf4j
public class RbacUserGroupServiceImpl implements RbacUserGroupService {

    @Autowired
    private RbacUserGroupMapper userGroupMapper;


    @Override
    public UserGroupEntity selectOne(String accountId, String appId, Integer status) {
        LambdaQueryWrapper<UserGroupEntity> where = Wrappers.lambdaQuery();
        where.eq(UserGroupEntity::getStatus, AccessConstant.NORMAL_STATUS)
                .eq(UserGroupEntity::getAccountId, accountId)
                .eq(UserGroupEntity::getAppId, appId)
                .isNull(UserGroupEntity::getDeletedAt)
                .last("LIMIT 1");
        return userGroupMapper.selectOne(where);
    }

    @Override
    public boolean addUser(GroupAddUserReqVO reqVO) {
        try {
            List<String> accountArr = JSONObject.parseArray(reqVO.getAccountId(), String.class);
            for (String accountId : accountArr) {
                LambdaQueryWrapper<UserGroupEntity> where = Wrappers.lambdaQuery();
                where.eq(UserGroupEntity::getGroupId, reqVO.getGroupId())
                        .eq(UserGroupEntity::getAppId, reqVO.getAppId())
                        .eq(UserGroupEntity::getAccountId, accountId)
                        .isNull(UserGroupEntity::getDeletedAt)
                        .last("LIMIT 1");
                UserGroupEntity userGroupEntity = userGroupMapper.selectOne(where);
                if (userGroupEntity == null) {
                    userGroupEntity = new UserGroupEntity();
                    userGroupEntity.setAccountId(accountId);
                    userGroupEntity.setGroupId(reqVO.getGroupId());
                    userGroupEntity.setAppId(reqVO.getAppId());
                    userGroupMapper.insert(userGroupEntity);
                }
            }
            return true;
        } catch (Exception e) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
    }

    @Override
    public boolean deleteUser(GroupDeleteUserReqVO reqVO) {
        try {
            List<String> accountIdList = JSONObject.parseArray(reqVO.getAccountId(), String.class);
            if (accountIdList.size() > 0) {
                LambdaUpdateWrapper<UserGroupEntity> updateWrapper = Wrappers.lambdaUpdate();
                updateWrapper.eq(UserGroupEntity::getGroupId, reqVO.getGroupId())
                        .in(UserGroupEntity::getAccountId, accountIdList)
                        .isNull(UserGroupEntity::getDeletedAt)
                        .set(UserGroupEntity::getStatus, AccessConstant.DEL_STATUS);
                userGroupMapper.update(null, updateWrapper);
            }
            return true;
        } catch (Exception e) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
    }
}
